
unset PATH
unset PACKAGE_CONF
unset PRODUCT_DIR

if [ $# -eq 1 ]
then
    PACKAGE_CONF=package.conf
    PRODUCT_DIRECTORY=${1}
elif [ $# -eq 2 ]
then
    PACKAGE_CONF=${1}
    PRODUCT_DIRECTORY=${2}
else
    PACKAGE_CONF=package.conf
    PRODUCT_DIRECTORY=helloworld    
fi

source ${PACKAGE_CONF}
source ${PRODUCT_DIRECTORY}/product.conf

${RM} --recursive --force ${PRODUCTION_DIRECTORY} ${WORKING_DIRECTORY}
${MKDIR} --parents ${WORKING_DIRECTORY}/src/${NAME}-${VERSION}
${CD} ${PRODUCT_DIRECTORY}/src
${ECHO} ${SOURCES}
${CP} --target-directory=${WORKING_DIRECTORY}/src/${NAME}-${VERSION} --parents makefile ${SOURCES}
${MKDIR} --parents ${WORKING_DIRECTORY}/tar
${TAR} --create --file=${WORKING_DIRECTORY}/tar/${NAME}-${VERSION}.tar --directory=${WORKING_DIRECTORY}/src ${NAME}-${VERSION}
${MKDIR} --parents ${PRODUCTION_DIRECTORY}/SOURCES
${GZIP} --to-stdout ${WORKING_DIRECTORY}/tar/${NAME}-${VERSION}.tar > ${PRODUCTION_DIRECTORY}/SOURCES/${NAME}-${VERSION}.tar.gz
${MKDIR} ${PRODUCTION_DIRECTORY}/SPECS
${ECHO} "Summary:   ${SUMMARY}"                                                      >  ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "License:   ${LICENSE}"                                                     >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "Name:      ${NAME}"                                                        >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "Version:   ${VERSION}"                                                     >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "Release:   ${RELEASE}"                                                     >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "Source:    ${NAME}-${VERSION}.tar.gz"                                      >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "Prefix:    ${PREFIX}"                                                      >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "Group:     ${GROUP}"                                                       >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "BuildRoot: ${PRODUCTION_DIRECTORY}/${NAME}-${VERSION}-root"                >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
for BUILD_REQUIRE in ${BUILD_REQUIRES}
do
    ${ECHO} "BuildRequires: ${BUILD_REQUIRE}"                                       >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
done
${ECHO}                                                                             >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "%description"                                                              >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "${DESCRIPTION}"                                                            >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO}                                                                             >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "%prep"                                                                     >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "%setup -q"                                                                 >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO}                                                                             >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "%build"                                                                    >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "${MAKE}"                                                                   >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO}                                                                             >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "%install"                                                                  >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "${MAKE} install PREFIX=\${RPM_BUILD_ROOT}"                                 >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO}                                                                             >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "%post"                                                                     >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "${MAKE} post PREFIX=\${RPM_BUILD_ROOT}"                                    >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO}                                                                             >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "%files"                                                                    >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
${ECHO} "%defattr(-,root,root)"                                                     >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
for INCLUDE in ${INCLUDES}
do
    ${ECHO} "/${INCLUDE}"                                                           >> ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec
done
${MKDIR} --parents ${PRODUCTION_DIRECTORY}/BUILD
${MKDIR} --parents ${PRODUCTION_DIRECTORY}/RPMS
${MKDIR} --parents ${PRODUCTION_DIRECTORY}/SRPMS
${RPMBUILD} -v -bb ${PRODUCTION_DIRECTORY}/SPECS/${NAME}-${VERSION}.spec